<template>
    <div>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="客户编号" prop="user_id">
                <el-input v-model="form.user_id" :disabled="disable"></el-input>
            </el-form-item>
            <el-form-item label="航班编号" prop="flight_id">
                <el-input v-model="form.flight_id" @change="changeFlight"></el-input>
            </el-form-item>
            <div v-for="(user,index) in userList" :key="index">
                <el-form-item label="乘客编号">
                    <el-input v-model="user.user_name"></el-input>
                </el-form-item>
                <el-form-item label="乘客证件">
                    <el-input v-model="user.user_idCard"></el-input>
                </el-form-item>
                <el-form-item label="联系方式">
                    <el-input v-model="user.user_phone"></el-input>
                </el-form-item>
                <el-form-item label="座位类型" prop="site_type">
                <el-select v-model="user.site_type" placeholder="请选择座位类型">
                    <el-option label="经济舱" value="0"></el-option>
                    <el-option label="商务舱" value="1"></el-option>
                    <el-option label="头等舱" value="2"></el-option>
                </el-select>
            </el-form-item>
            </div>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="form.remark"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addOrder">立即创建</el-button>
                <el-button @click="addUser">添加乘机人</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    name:'order-editor',
    props:{
        form:{
            type:Object
        },
        disable:{
            type:Boolean
        }
    },
    data(){
        return{
            rules:{
                user_id:[
                    { required: true, message: '请输入用户编号', trigger: 'blur' }
                ],
                flight_id:[
                    { required: true, message: '请输入航班编号', trigger: 'blur' }
                ]
            },
            flight:{},
            userList:[{
                user_name:'',
                site_type:'',
                user_idCard:'',
                user_phone:''
            }],
            price:[]
        }
    },
    methods:{
        addOrder(){
            let item = this.form
            if(this.checkEmpty(item.user_id,"客户编号") || this.checkEmpty(item.flight_id,"航班编号")){
                return;
            }
            item.admin_id = this.$cookies.get("admin").admin_id;
            item.touristClassNum = 0;
            item.businessClassNum = 0;
            item.firstClassNum = 0;
            let total = 0
            let flight = this.flight;
            let price = 0;
            for(let i = 0;i<this.userList.length;i++){
                let user = this.userList[i]
                switch (user.site_type){
                    case "0":
                        price = parseInt(flight.touristClassPrice)
                        item.touristClassNum++;
                        break;
                    case "1":
                        price = parseInt(flight.businessClassPrice)
                        item.businessClassNum++;
                        break;
                    case "2":
                        price = parseInt(flight.firstClassPrice)
                        item.firstClassNum++;
                        break;
                }
                total += price
            }
            item.totalPrice = total;
            console.log(item);
            console.log(this.userList);
            this.$http.post("/api/order/addOrder",item).then((res)=>{
                console.log(res);
                if(this.checkCode(res.status)){
                    this.showMessage('创建成功！','success');
                    for(let i=0;i<this.userList.length;i++){
                        let user = this.userList[i]
                        user.order_id = res.data.order_id
                        user.flight_id = item.flight_id
                        console.log("user",user);
                        this.$http.post("/api/ticket/addTicket",user).then((res)=>{
                            console.log("userRes",res);
                        })
                    }
                    this.$emit('closeDialog');
                    this.$emit('refresh');
                }else{
                    this.showMessage('创建失败！','error');
                }
            })
        },
        changeFlight(){
            this.$http.get(`/api/flight/getFlight?flight_id=${this.form.flight_id}`).then((res)=>{
                console.log(res);
                if(this.checkCode(res.status)){
                    this.flight = res.data.data;
                    this.price[0] = this.flight.touristClassPrice;
                    this.price[1] = this.flight.businessClassPrice;
                    this.price[2] = this.flight.firstClassPrice;
                }
            })
        },
        multiPrice(item,flight){
            return parseInt(item) * parseInt(flight) || 0;
        },
        addUser(){
            this.userList.push({
                user_name:'',
                site_type:'',
                user_idCard:'',
                user_phone:''
            })
        }
    }
}
</script>